{% extends "appbuilder/general/model/show.html" %}

{% block content %}
{{ super() }}

<div class="panel panel-default">
    <div class="panel-heading">
        <h3 class="panel-title">{{ model.name }} - 依赖关系管理</h3>
    </div>
    <div class="panel-body">
        <!-- 自动分析按钮 -->
        <div class="row">
            <div class="col-md-12">
                <button type="button" class="btn btn-primary" onclick="analyzeAutoDependencies()">自动分析依赖关系</button>
                <button type="button" class="btn btn-info" onclick="showAddDependencyModal()">手动添加依赖</button>
            </div>
        </div>
        
        <hr>
        
        <!-- 现有依赖关系列表 -->
        <div class="row">
            <div class="col-md-12">
                <h4>现有依赖关系</h4>
                <table class="table table-striped">
                    <thead>
                        <tr>
                            <th>目标模型</th>
                            <th>依赖类型</th>
                            <th>依赖强度</th>
                            <th>描述</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for dep in dependencies %}
                        <tr>
                            <td>{{ dep.target_model.name }}</td>
                            <td>{{ dep.dependency_type }}</td>
                            <td>{{ dep.dependency_strength }}</td>
                            <td>{{ dep.description }}</td>
                            <td>
                                <button class="btn btn-sm btn-danger" onclick="deleteDependency({{ dep.id }})">删除</button>
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

<!-- 添加依赖关系模态框 -->
<div class="modal fade" id="addDependencyModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title">添加依赖关系</h4>
                <button type="button" class="close" data-dismiss="modal">&times;</button>
            </div>
            <form method="post">
                <div class="modal-body">
                    <input type="hidden" name="action" value="add">
                    
                    <div class="form-group">
                        <label>目标模型:</label>
                        <select name="target_model_id" class="form-control" required>
                            <option value="">请选择模型</option>
                            {% for model in available_models %}
                            <option value="{{ model.id }}">{{ model.name }} ({{ model.version }})</option>
                            {% endfor %}
                        </select>
                    </div>
                    
                    <div class="form-group">
                        <label>依赖类型:</label>
                        <select name="dependency_type" class="form-control" required>
                            <option value="model_inheritance">模型继承</option>
                            <option value="pipeline_flow">Pipeline流程</option>
                            <option value="shared_dataset">共享数据集</option>
                            <option value="version_evolution">版本演进</option>
                            <option value="manual">手动配置</option>
                        </select>
                    </div>
                    
                    <div class="form-group">
                        <label>依赖强度:</label>
                        <input type="number" name="dependency_strength" class="form-control" 
                               min="0" max="1" step="0.1" value="1.0" required>
                    </div>
                    
                    <div class="form-group">
                        <label>描述:</label>
                        <textarea name="description" class="form-control" rows="3"></textarea>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
                    <button type="submit" class="btn btn-primary">添加</button>
                </div>
            </form>
        </div>
    </div>
</div>

<script>
function showAddDependencyModal() {
    $('#addDependencyModal').modal('show');
}

function deleteDependency(depId) {
    if (confirm('确定要删除这个依赖关系吗？')) {
        const form = document.createElement('form');
        form.method = 'POST';
        form.innerHTML = `
            <input type="hidden" name="action" value="delete">
            <input type="hidden" name="dependency_id" value="${depId}">
        `;
        document.body.appendChild(form);
        form.submit();
    }
}

function analyzeAutoDependencies() {
    if (confirm('确定要重新分析依赖关系吗？这将覆盖现有的自动生成的依赖关系。')) {
        fetch(`/training_model_modelview/api/analyze_dependencies/{{ model.id }}`, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            }
        })
        .then(response => response.json())
        .then(data => {
            if (data.success) {
                alert('依赖关系分析完成');
                location.reload();
            } else {
                alert('分析失败: ' + data.message);
            }
        })
        .catch(error => {
            alert('分析失败: ' + error.message);
        });
    }
}
</script>
{% endblock %}